package com.qin.springbootdemo.dao;

import com.qin.springbootdemo.entity.cartlistEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Leisure
 * create 2022 2022/6/18 11:18
 **/
@Repository
public class cartsDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    //查询购物车
    public List<cartlistEntity> findCartAll() {
        List<cartlistEntity> cartlist = mongoTemplate.findAll(cartlistEntity.class,"cartlist");

        return cartlist;
    }
    //加入购物车
    public cartlistEntity insertCart(cartlistEntity cart) {
        this.mongoTemplate.save(cart, "cartlist");
        return null;
    }
    //删除商品
    public void delCart(String id) {
        //先查询出来数据 这里的is(id)可以改成in(ids) 即为批量删除
        Query query = new Query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query, cartlistEntity.class);
    }
    //更新购物车
    public cartlistEntity updateCart(cartlistEntity cart) {
        // TODO Auto-generated method stub
        Criteria criteria = Criteria.where("_id").is(cart.get_id());
        Query query = Query.query(criteria);
        Update update = new Update();
        // 修改特定字段
        update.set("num", cart.getNum());

        mongoTemplate.updateFirst(query, update, cartlistEntity.class);
        return null;
    }

}
